﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using TransformerManager.Shared.Dto.OperationLog;
using TransformerManager.Shared.Helper.HttpParams;
using hc = HandyControl.Controls;

namespace TransformerManager.WPF.Common.Http.Service
{
    public class OperationLogService : IOperationLogService
    {
        private readonly HttpRequest _httpRequest;
        public OperationLogService(HttpRequest httpRequest)
        {
            _httpRequest = httpRequest;
        }

        public async Task<HeaderResponse<List<OperationLogsDto>>> GetOperationLogsAsync(
            string userName, 
            DateTime operateTimeStart, 
            DateTime operateTimeEnd, 
            int pageIndex, 
            int pageSize)
        {
            string url = $"OperationLog/Query?userName={userName}" +
                $"&operateTimeStart={operateTimeStart}&operateTimeEnd={operateTimeEnd}" +
                $"&pageNumber={pageIndex}&pageSize={pageSize}";
            var responseMsg = await _httpRequest.RequestAsync(HttpMethod.Get, url, null);
            var content = await responseMsg.Content.ReadAsStringAsync();
            var headers = responseMsg.Headers;
            var result = JsonConvert.DeserializeObject<ServiceResponse<List<OperationLogsDto>>>(content);
            if (!result.Success)
            {
                hc.Growl.Error(
                        $"响应信息: {result.Message}",
                        "Notification");
                return new HeaderResponse<List<OperationLogsDto>>
                {
                    Data = new List<OperationLogsDto>(),
                    Headers = headers
                };
            }
            return new HeaderResponse<List<OperationLogsDto>>
            {
                Data = result.Data,
                Headers = headers
            };
        }

        public async Task<string> GetRequestBodyByIdAsync(int id)
        {
            string url = $"OperationLog/RequestBody/{id}";
            var responseMsg = await _httpRequest.RequestAsync(HttpMethod.Get, url, null);
            var content = await responseMsg.Content.ReadAsStringAsync();
            var result = JsonConvert.DeserializeObject<ServiceResponse<string>>(content);
            if (!result.Success)
            {
                hc.Growl.Error(
                        $"响应信息: {result.Message}",
                        "Notification");
                return result.Data;
            }
            return result.Data;
        }
    }
}
